const userModel = require('../model/userModel.js');
const userController ={
    login: async (req,res) =>{
        try {
            const result = await  userModel.login(req.body);
            if(!!result){
                res.send({
                    code:1,
                    msg:'用户登录成功！',
                    id:result
                })
            }else {
                res.send({
                    code:0,
                    msg:'用户登录失败，请检查账号密码！',

                })
            }
        }catch (error){
            console.log(error,'controller层用户登录出错啦')
        }
    },
    register: async (req,res) =>{
        try {
            const result = await userModel.register(req.body,req.file);
            if(result == 0){
                res.send({
                    code:0,
                    msg:"用户名已存在，请重新注册"
                });
            }else if(result == 1){
                res.send({
                    code:1,
                    msg:"用户注册成功！"
                });
            }else if(result.length == 0){
                res.send({
                    code:2,
                    msg:"输入参数有误，请稍后再试！"
                });
            }
        } catch (error) {
            console.log(error,'注册用户出错啦！');
        }
    },
    GetUserList: async (req, res) => {
        console.log(req.body,'controller层获取用户列表调试');
        try {
            const {list,total} = await userModel.GetUserList(req.body);
            console.log(list,total,'用户列表响应参数');
            if(list.length > 0){
                res.status(200).send({
                    code: 1,
                    msg: "成功获取用户列表",
                    list,
                    total,
                });
            }else{
                res.status(404).send({
                    code: 0,
                    msg: "获取用户列表失败，请输入正确的参数",
                });
            }
        } catch (error) {
            console.log(error,"获取用户列表出现error");
        };
    },
    AddUser: async (req, res) => {
        try {
            const result = await userModel.AddUser(req.body,req.file);
            if(result == 0){
                res.send({
                    code:0,
                    msg:"用户名已存在，请重新添加"
                });
            }else if(result == 1){
                res.send({
                    code:1,
                    msg:"添加用户成功！"
                });
            }else if(result.length == 0){
                res.send({
                    code:2,
                    msg:"输入参数有误，请稍后再试！"
                });
            }
        } catch (error) {
            console.log(error,'添加用户出错啦！');
        }
    },
    EditUser :async (req,res) => {
        try {
            const result = await userModel.EditUser(req.body,req.file);
            if(result === 1){
                res.send({
                    code:1,
                    msg:"修改用户成功！"
                });
            }else {
                res.send({
                    code:2,
                    msg:"输入参数有误，请稍后再试！"
                });
            }
        } catch (error) {
            console.log(error,'修改用户出错啦！');
        }
    },
    DeleteUser: async (req, res) => {
        try {
            const result = await userModel.DeleteUser(req.body);
            if (result) {
                res.status(200).send({
                    code: 1,
                    msg: "删除用户成功",
                });
            } else {
                res.status(404).send({
                    code: 0,
                    msg: "删除用户失败",
                });
            }
        } catch (error) {
            console.log(error, '删除用户出现error');
        };
    },
    BatchDeleteUser: async (req,res)=>{
        try {
            let result = await userModel.BatchDeleteUser(req.body);
            if(result.affectedRows !== 0){
                res.status(200).send({
                    code:1,
                    msg:"批量删除用户成功",
                });
            }else{
                res.status(404).send({
                    code:0,
                    msg:"批量删除用户失败",
                });
            }
        }catch (error){
            console.log(error,'controller层批量删除用户出现error');
        }
    },
    GetUserDetail:async (req,res) => {
        try{
            const result = await userModel.GetUserDetail(req.params);
            console.log(result,'user-controller结果');
            if(result.length >0){
                res.status(200).send({
                    code:1,
                    detail:result[0],
                    msg:"获取用户详情成功"
                })
            }else{
                res.status(404).send({
                    code:0,
                    msg:"获取用户详情失败",
                });
            }
        }catch (error){
            console.log(error,'获取用户详情出现error');
        }
    }

};


module.exports = userController;